package com.ruoyi.web.controller.parts;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ProductType;
import com.ruoyi.system.domain.vo.productCar.ProductCarListVo;
import com.ruoyi.system.service.IProductTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProductCar;
import com.ruoyi.system.service.IProductCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品-汽车适配（中间）Controller
 *
 * @author ruoyi
 * @date 2023-01-25
 */
@RestController
@RequestMapping("/parts/productCar")
public class ProductCarController extends BaseController
{
    @Autowired
    private IProductCarService productCarService;

    @Autowired
    private IProductTypeService productTypeService;

    /**
     * 查询产品-汽车适配（中间）列表
     */
    @PreAuthorize("@ss.hasPermi('system:productcar:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductCarListVo productCar)
    {
        startPage();
        List<ProductCarListVo> list = productCarService.selectProductCarVoList(productCar);
        TableDataInfo dataTable = getDataTable(list);
        List<ProductCarListVo> rows = (List<ProductCarListVo>) dataTable.getRows();

        Map<Long, ProductType> typeMap = productTypeService.selectProductTypeMap(null);
        List<ProductCarListVo> tempRows  = rows.stream().map((vo)->{
            String name = typeMap.get(vo.getProductTypeId()).getName();
            vo.setProductTypeName(name);
            return vo;
        }).collect(Collectors.toList());
        dataTable.setRows(tempRows);
        return dataTable;
    }

    /**
     * 导出产品-汽车适配（中间）列表
     */
    @PreAuthorize("@ss.hasPermi('system:productcar:export')")
    @Log(title = "产品-汽车适配（中间）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCar productCar)
    {
        List<ProductCar> list = productCarService.selectProductCarList(productCar);
        ExcelUtil<ProductCar> util = new ExcelUtil<ProductCar>(ProductCar.class);
        util.exportExcel(response, list, "产品-汽车适配（中间）数据");
    }

    /**
     * 获取产品-汽车适配（中间）详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:productcar:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productCarService.selectProductCarById(id));
    }

    /**
     * 新增产品-汽车适配（中间）
     */
    @PreAuthorize("@ss.hasPermi('system:productcar:add')")
    @Log(title = "产品-汽车适配（中间）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCar productCar)
    {
        return toAjax(productCarService.insertProductCar(productCar));
    }

    /**
     * 修改产品-汽车适配（中间）
     */
    @PreAuthorize("@ss.hasPermi('system:productcar:edit')")
    @Log(title = "产品-汽车适配（中间）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCar productCar)
    {
        return toAjax(productCarService.updateProductCar(productCar));
    }

    /**
     * 删除产品-汽车适配（中间）
     */
    @PreAuthorize("@ss.hasPermi('system:productcar:remove')")
    @Log(title = "产品-汽车适配（中间）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productCarService.deleteProductCarByIds(ids));
    }
}
